package org.example.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.entity.SysBlank;
import org.example.entity.SysClient;
import org.example.server.BankServer;
import org.example.server.ClientBlankServer;
import org.example.server.ClientServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("sys-client")
public class ClientController {
    @Autowired
    ClientServer clientServer;
    @Autowired
    BankServer bankServer;
    @Autowired
    ClientBlankServer clientBlankServer;
    /**
     * 查询客户列表方法
     * @return
     */
    @RequestMapping("clientList")
    public PageInfo<SysClient> clientList(@RequestBody SysClient sysClient){
        PageHelper.startPage(sysClient.getPageNum(),sysClient.getPageSize());

        List<SysClient> list=clientServer.clientList(sysClient.getSex(),sysClient.getXname());
        final PageInfo<SysClient> sysClientPageInfo = new PageInfo<>(list);
        return sysClientPageInfo;
    }
    /*根据id查询客户详情*/
    @RequestMapping("clientDetails")
    public SysClient clientDetails(Integer id){
        return clientServer.clientDetails(id);
    }
    @RequestMapping("upload")
    public String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        final String substring = originalFilename.substring(originalFilename.lastIndexOf("."), originalFilename.length());
        System.out.println("图片的后缀：====="+substring);
        String filename= UUID.randomUUID()+substring;
        System.out.println("图片名称："+filename);
        file.transferTo(new File("/Users/zhaowenting/Desktop/upload/pic/"+filename));
        return "http://172.20.10.5:8087/goods/upload/pic/"+filename;
    }
    /**
     * 银行列表
     */
    @RequestMapping("blankList")
    public List<SysBlank> blankData(){
        List<SysBlank> list=bankServer.blankList();
        return list;
    }
    /**
     * 添加客户
     */
    @RequestMapping("addClient")
    public String addClient(@RequestBody SysClient sysClient){
        System.out.println("前端传过来的信息：=="+sysClient);
        clientServer.save(sysClient);
        System.out.println("id===="+sysClient.getId());
        SysClient dbSysClient=clientServer.selectByName(sysClient.getName());
        if(sysClient.getBlankIds()!=null){
            //clientBlankServer.deleteById(sysClient.getId());
            for (Integer blankId : sysClient.getBlankIds()) {
                System.out.println("银行id:"+blankId);
                clientBlankServer.save(dbSysClient.getId(),blankId);
            }
        }
        return "success";
    }

    /**
     * 根据客户id去客户与银行的中间表查询银行id
     */
    @RequestMapping("getBlankIdsByClientId")
    public Integer[] getBlankIdsByClientId(Integer id){
        Integer[] a=clientBlankServer.getBlankIdsByClientId(id);
        return a;
    }
    /**
     * 修改客户方法
     */
    @RequestMapping("updateClient")
    public String updateClient(@RequestBody SysClient sysClient) {
        if(sysClient.getBlankIds()!=null){
            clientBlankServer.deleteById(sysClient.getId());
            for (Integer blankId : sysClient.getBlankIds()) {
                System.out.println("银行id:"+blankId);
                clientBlankServer.save(sysClient.getId(),blankId);
            }
        }
        clientServer.updateById(sysClient);
        return "success";
    }
    /**
     * 根据id去删除
     */
    @RequestMapping("deleteClient")
    public String deleteClient(Integer id){
        clientServer.deleteClient(id);
        return "success";
    }
    /**
     * 批量删除
     */
    @RequestMapping("batchDelete")
    public String batchDelete(Integer[] clientIds){
        clientServer.batchDelete(clientIds);
        return "success";
    }
}
